package com.zhiyou100.controller;

import com.zhiyou100.entity.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常拦截器
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-08 11:42
 * ControllerAdvice 是一个异常拦截器
 * RestControllerAdvice = ControllerAdvice + ResponseBody
 */
@RestControllerAdvice
public class ExceptionController {
    /**
     * ExceptionHandler 指定拦截异常的类型
     * ResponseBody 响应 json 串
     *
     * @param error 异常
     * @return 响应
     */
    @ExceptionHandler(Throwable.class)
    public ResponseEntity<Object> error( Throwable error) {
        return ResponseEntity.builder()
                .status(500)
                .message(error.getMessage())
                .obj(error)
                .build();
    }
}
